/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 18:42:02
 ******************************************************************************/
package cn.com.xy.sms.sdk.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.entity.Sceneconfig;
import cn.com.xy.sms.sdk.db.entity.SceneconfigManager;
import cn.com.xy.sms.sdk.log.LogManager;

public class SceneconfigDownloaderQueue extends Thread {

    static boolean isRun = false;// 是否正在下载中

    public synchronized static void runSceneconfigDownloadQueue() {
        // if(XyUtil.checkNetWork(Constant.getContext()) == 0)
        {
            if (isRun == false) {
                // 当队列线程序未运行时,才运行
                SceneconfigDownloaderQueue queue = new SceneconfigDownloaderQueue();
                queue.start();
            }
        }

    }

    @Override
    public void run() {
        try {
            if (isRun == false) {
                isRun = true;
                try {

                    Thread.sleep(20000);
                    ZipPackageDownloaderQueue.runDownResQueue(false);
                    Thread.sleep(10000);

                } catch (Throwable e) {
                    LogManager.e(Constant.TAG, "run: ", e);

                }

                requestQue(SceneconfigManager.SCENE_TYPE_POPUP);
                requestQue(SceneconfigManager.SCENE_TYPE_BUBBLE);

                XmlDownloaderQueue.runDownResQueue(false);

                // 检测是否需要情景、情景规则升级
                SceneconfigUtil.updateData();

                isRun = false;

            }
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "run: ", e);

        }

    }

    public void requestQue(int sceneType) {
        List<Sceneconfig> queuelist = SceneconfigManager.queryAllSceneconfig(
                sceneType, SceneconfigManager.NOTFINISH);
        if (queuelist != null && !queuelist.isEmpty()) {
            SceneconfigUtil.insertOrUpdateSceneConfigAndRequestScenceConfig(queuelist, sceneType, false);

        }
    }

    public static void runTask(String time) {
        try {
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                	try {
                        SceneconfigDownloaderQueue.runSceneconfigDownloadQueue();
					} catch (Throwable e) {
			            LogManager.e(Constant.TAG, "run: ", e);
					}


                }
            };

            Timer timer = new Timer();
            Date d = new Date();
            Calendar cal = Calendar.getInstance();

            String date = cal.get(Calendar.YEAR) + "/"
                    + (cal.get(Calendar.MONTH) + 1) + "/"
                    + cal.get(Calendar.DAY_OF_MONTH);

            date += " " + time;

            try {
                d = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(date);
            } catch (Throwable e) {
//                e.printStackTrace();
                LogManager.e(Constant.TAG, "run: " , e);

            }
            timer.schedule(task, d);
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "run: ", e);

        }

    }
}
